/****************************************************************************
 * Copyright 2009 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.filter.reflection;

import com.gotobject.filter.Filter;
import com.gotobject.filter.FilterImpl;
import com.gotobject.filter.predicate.Predicate;
import com.gotobject.filter.spi.ContextFluency;

import static com.gotobject.filter.common.Aggregations.count;
import static com.gotobject.filter.common.Iterables.asList;
import static com.gotobject.filter.common.Predicates.annotatedWith;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
@SuppressWarnings({"ALL"})
public class ReflectionFilterTest {
    @Test
    public void testSelectionOfMethodsAnnotatedWithTestAnnotation() throws Exception {        
        final Iterable<Method> m = new ReflectionFilter(makeFilter(true)).from(getClass()).methods().select(annotatedWith(Test.class));
        final Method targetMethod = getClass().getDeclaredMethod("testSelectionOfMethodsAnnotatedWithTestAnnotation");
        assertEquals("both methods should be equal", targetMethod, asList(m).get(0));
    }


    @Test
    public void testSelectionOfAnnotatedFields() throws Exception {
        final Field             tField  = CoolClass.class.getDeclaredField("name");
        final Iterable<Field>   f       = new ReflectionFilter(makeFilter(true)).from(CoolClass.class).fields().select(annotatedWith(Cool.class));

        assertEquals("both fields should be equal", tField, asList(f).get(0));
        
    }

    @Test
    public void testSelectionOfDeclaredAnnotations() throws Exception {
        final Iterable<Annotation>  a       = new ReflectionFilter(makeFilter(true)).from(HappyClass.class).annotations().select();
        assertEquals("both fields should be equal", 1, count(a));
    }


    @Test
    public void testSelectionOfDeclaredConstructor() throws Exception {
        final Iterable<Constructor<?>>  a       = new ReflectionFilter(makeFilter(true)).from(HappyClass.class).constructors().select();
        assertEquals("both fields should be equal", 1, count(a));
    }

    @Test
    public void testSelectionOfClass_Nested_Inner() throws Exception {
        final Iterable<Class<?>> c = new ReflectionFilter().from(SuperClass.class).classes().select();
        final int i = 0;
    }

    @Retention(RetentionPolicy.RUNTIME)
    private @interface Cool {}

    @Cool
    private static class CoolClass {
        @Cool private String name = "Me";
        @Override public String toString(){
            return name;
        }
    }

    @Retention(RetentionPolicy.RUNTIME)
    public @interface Happy {}

    @Happy
    static class HappyClass {
        HappyClass(){}
    }


    private static class SuperClass {
        static class NestedClass{}
        class InnerClass{}
    }

    private static ContextFluency makeContextFluency(final boolean value) {
        final ContextFluency check = new ContextFluency(){
            @Override
            public boolean isSelect(Predicate<?> cond) {
                return value;
            }
        };

        return check;
    }

    private static Filter makeFilter(boolean predicateValue) {
        return new FilterImpl(new ArrayList<Throwable>(), makeContextFluency(predicateValue));
    }

}
